﻿using Newtonsoft.Json;
using PMSWenLvTing.BLL;
using PMSWenLvTing.Model;
using PMSWenLvTing.Model.JiuDian;
using PMSWenLvTing.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace PMSWenLvTing.Controllers
{
    public class JiuDianController : BaseJDController
    {
        MainBLL bllm = new MainBLL();

        //待填报
        public ActionResult JDDaiTianBao()
        {
            ViewBag.lisjd = JsonConvert.SerializeObject(Self_GetMyJDShort());
            return View();
        }

        //已填报
        public ActionResult JDYiTianBao()
        {
            ViewBag.lisjd = JsonConvert.SerializeObject(Self_GetMyJDShort());
            return View();
        }

        //账户管理
        public ActionResult JDZhangHu()
        {
            return View();
        }

        //账户管理--添加子账号--编辑子账号
        public ActionResult JDZhangHuShow()
        {
            #region 存放方法变量
            //图片验证码，调用同一个发送短信的方法时验证用
            Session["checkcode"] = "0000";

            string yhjdid_str = Request["yhjdid"];
            int yhjdid = -1; 
            #endregion

            #region 获取主账号酒店数据
            yonghu_jiudian modelyh_mother = Self_GetMotherJDYH();
            if (modelyh_mother == null)
            {
                return Content("<script>alert('主账号不存在，请注册主账号后添加子账号');history.back();</script>");
            }
            else if (modelyh_mother.id != GetCurrentUser().id)
            {
                return Content("<script>alert('抱歉！您无权操作');history.back();</script>");
            }
            List<jiudian> lisjd_mother = bllm.Get_jiudian(JsonConvert.DeserializeObject<List<int>>(modelyh_mother.jdid_json), false);
            if (lisjd_mother == null)
            {
                lisjd_mother = new List<jiudian>();
            }
            #endregion

            if (int.TryParse(yhjdid_str, out yhjdid) == false)
            {
                #region 添加子账号
                DTO_yonghu_jiudian modelyh_res = new DTO_yonghu_jiudian();
                //获取主账号绑定的酒店数据，并解析成酒店短信息
                List<DTO_JDShort> lisjd_short_mother = new List<DTO_JDShort>();
                
                lisjd_short_mother = ToDTO.Self_JDToJDShort(lisjd_mother);
                modelyh_res.lisjd = lisjd_short_mother;
                modelyh_res.lisjdid = new List<int>();
                //界面传值
                ViewBag.modelyh = JsonConvert.SerializeObject(modelyh_res);
                ViewBag.sub_title = "新建子账户";
                #endregion
            }
            else
            {
                #region 修改账号
                List<yonghu_jiudian> lisyhjd = bllm.Get_yonghu_jiudian(string.Format("id={0}", yhjdid));
                if (lisyhjd == null || lisyhjd.Count <= 0)
                {
                    return JavaScript("<script>alert('无相关用户数据，请重新选择');history.back();</script>");
                }
                List<DTO_yonghu_jiudian> lisyhjd_dto = ToDTO.Self_ToYHJDShow(lisjd_mother, lisyhjd);
                //界面传值
                ViewBag.modelyh = JsonConvert.SerializeObject(lisyhjd_dto.First());
                ViewBag.sub_title = "编辑账号信息";
                #endregion
            }
            return View();
        }

        //酒店信息
        public ActionResult JDInfo()
        {
            return View();
        }

        //酒店信息 详情展示
        public ActionResult JDInfoDetailShow()
        {
            #region 获取并检查酒店数据
            string jdid_str = Request["jdid"];
            int jdid = -1;
            if (int.TryParse(jdid_str, out jdid) == false)
            {
                return Content("<script> alert('无效酒店，请重新查看！');history.back();</script>");
            }
            List<jiudian> lisjd = Self_GetMyJiuDian();
            if (lisjd == null || lisjd.Count <= 0)
            {
                return Content("<script> alert('无效酒店，请重新查看！');history.back();</script>");
            }
            else if (lisjd.Exists(kkm => kkm.id == jdid) == false)
            {
                return Content("<script> alert('无效酒店，请重新查看！');history.back();</script>");
            }
            #endregion

            #region 转换成界面展示数据
            DTO_jiudian modelres = new DTO_jiudian();
            modelres = JsonConvert.DeserializeObject<DTO_jiudian>(JsonConvert.SerializeObject(lisjd.Where(kkm=>kkm.id==jdid).First()));
            modelres.shenhe_shijian = CommonHelper.Self_RiQiToStr2000(lisjd.First().shenhe_shijian,true);
            modelres.shenqing_shijian= CommonHelper.Self_RiQiToStr2000(lisjd.First().shenqing_shijian, true);
            modelres.kaiye_riqi= CommonHelper.Self_RiQiToStr2000(lisjd.First().kaiye_riqi, false);
            modelres.pingxing_riqi= CommonHelper.Self_RiQiToStr2000(lisjd.First().pingxing_riqi, false);

            ViewBag.modeljdstr = JsonConvert.SerializeObject(modelres);
            #endregion

            return View();
        }

        //子酒店注册页面 
        public ActionResult JDSubZhuCeView()
        {
            DTO_jiudian modeljd = new DTO_jiudian();
            string imginit = "../../image/icon_zc.jpg";
            modeljd.img_yingye_zhizhao = imginit;
            modeljd.img_faren_shenfenzheng_fan = imginit;
            modeljd.img_faren_shenfenzheng_zheng = imginit;
            modeljd.img_shenqingren_sfz_fan = imginit;
            modeljd.img_shenqingren_sfz_zheng = imginit;
            modeljd.img_tezhong_hangye = imginit;
            ViewBag.modeljdstr = JsonConvert.SerializeObject(new DTO_jiudian());
            //图片验证码，调用同一个发送短信的方法时验证用
            Session["checkcode"] = "0000";
            return View();
        }

        //获取报表信息
        public ActionResult GetBaoBiaoP()
        {
            #region 存放方法变量
            //bb=baobiao报表
            string pageIndex_str = Request["pageIndex"];
            string pageSize_str = Request["pageSize"];
            string jdid_str = Request["jdid"];
            string bblx_str = Request["bblx"];
            string bbdate1_str = Request["bbdate1"];
            string bbdate2_str = Request["bbdate2"];
            //填报状态 0待填报 1已填报
            string zttb_str = Request["zttb"];
            DateTime bbdate1 = DateTime.Now;
            DateTime bbdate2 = DateTime.Now;
            int jdid = -1;
            int bblx = -1;
            int pageIndex = 1;
            int pageSize = 10;
            int zttb = 0;
            bool bbdate1_ok = true;
            bool bbdate2_ok = true;

            //初始化结果数据
            DTO_BBZTShowAll modelRes = new DTO_BBZTShowAll();
            modelRes.pageHtml = "";
            modelRes.lisbb = new List<DTO_BBZTShow>();
            modelRes.pageIndex = 1;
            modelRes.pageSize = 10;
            #endregion

            #region 检查数据有效性
            if (int.TryParse(zttb_str, out zttb) == false)
            {
                return Json(modelRes);
            }
            if (int.TryParse(pageIndex_str, out pageIndex) == false)
            {
                pageIndex = 1;
            }
            if (int.TryParse(pageSize_str, out pageSize) == false)
            {
                pageSize = 10;
            }
            if (int.TryParse(jdid_str, out jdid) == false)
            {
                return Json(modelRes);
            }
            if (int.TryParse(bblx_str, out bblx) == false)
            {
                return Json(modelRes);
            }
            if (int.TryParse(bblx_str, out bblx) == false)
            {
                return Json(modelRes);
            }
            if (bblx == 1)
            {
                bbdate1_str += "-01";
                bbdate2_str += "-01";
            }
            else if (bblx == 2)
            {
                bbdate1_str += "-01-01";
                bbdate2_str += "-01-01";
            }
            if (DateTime.TryParse(bbdate1_str, out bbdate1) == false)
            {
                bbdate1_ok = false;
            }
            if (DateTime.TryParse(bbdate2_str, out bbdate2) == false)
            {
                bbdate2_ok = false;
            }
            #endregion

            #region 获取数据
            List<string> lisWhere = new List<string>();

            #region 酒店数据
            List<jiudian> lisjd = Self_GetMyJiuDian();
            if (lisjd == null || lisjd.Count <= 0)
            {
                return Json(modelRes);
            }
            if (jdid == -1)
            {
                lisWhere.Add(string.Format("jdid in ({0})", string.Join(",", lisjd.Select(kkm => kkm.id))));
            }
            else
            {
                if (lisjd.Exists(kkm => kkm.id == jdid) == false)
                {
                    return Json(modelRes);
                }
                lisWhere.Add(string.Format("jdid={0}", jdid));
            }
            #endregion

            #region 报表类型
            if (bblx != -1)
            {
                lisWhere.Add(string.Format("leixing={0}", bblx));
            }
            #endregion

            #region 填报状态
            if (zttb == 0)
            {
                lisWhere.Add(string.Format("(zt_tianbao={0} or zt_shenhe=2)", zttb));
            }
            else
            {
                lisWhere.Add(string.Format("zt_tianbao={0}", zttb));
            }
            
            #endregion

            #region 报表日期
            if (bbdate1_ok == false && bbdate2_ok == false)
            {
                //所有时间范围
            }
            else if (bbdate1_ok == true && bbdate2_ok == true)
            {
                //有限时间范围
                lisWhere.Add(string.Format("bb_riqi>='{0}' and bb_riqi<='{1}'", bbdate1.ToString("yyyy-MM-dd"), bbdate2.ToString("yyyy-MM-dd")));
            }
            else if (bbdate1_ok == true && bbdate2_ok == false)
            {
                //有开始时间
                lisWhere.Add(string.Format("bb_riqi>='{0}'", bbdate1.ToString("yyyy-MM-dd")));
            }
            else if (bbdate1_ok == false && bbdate2_ok == true)
            {
                //有结束时间
                lisWhere.Add(string.Format("bb_riqi<='{0}'", bbdate2.ToString("yyyy-MM-dd")));
            }
            #endregion

            #region 已删除
            lisWhere.Add("isdel=false");
            #endregion



            #region 获取总条数
            string strWhere = string.Join(" and ", lisWhere);
            int totalCount = bllm.Get_BBZhuangTaiCount(strWhere);
            if (totalCount <= 0)
            {
                return Json(modelRes);
            }
            #endregion

            #region 获取报表状态数据
            if (lisWhere.Count <= 0)
            {
                strWhere = string.Format("1=1 limit {0},{1}", (pageIndex - 1) * pageSize, pageSize);
            }
            else
            {
                strWhere += string.Format(" limit {0},{1}", (pageIndex - 1) * pageSize, pageSize);
            }

            List<baobiao_zhuangtai> lisbbzt = bllm.Get_baobiao_zhuangtai(strWhere);

            #endregion

            #region 获取报表发布数据
            List<baobiao_fabu> lisbbfb = new List<baobiao_fabu>();
            lisbbfb = bllm.Get_baobiao_fabu(string.Format("id in ({0})", string.Join(",", lisbbzt.Select(kkm => kkm.bbid))));
            if (lisbbfb == null)
            {
                lisbbfb = new List<baobiao_fabu>();
            }
            #endregion

            #region 获取用户数据
            List<yonghu_jiudian> lisyhjd = new List<yonghu_jiudian>();
            lisyhjd = bllm.Get_yonghu_jiudian(string.Format("id in ({0})", string.Join(",", lisbbzt.Select(kkm => kkm.yyjdid))));
            if (lisyhjd == null)
            {
                lisyhjd = new List<yonghu_jiudian>();
            }
            #endregion


            #endregion

            #region 分析数据
            List<DTO_BBZTShow> lisshow = ToDTO.Self_ToBBShow(lisjd, lisbbfb, lisbbzt, lisyhjd);
            modelRes.pageIndex = pageIndex;
            modelRes.pageSize = pageSize;
            modelRes.lisbb = lisshow;
            string redirectParam = string.Format("GetBaoBiaoP({0},{1},\"{2}\",\"{3}\",{4}", jdid_str, bblx_str, bbdate1_str, bbdate2_str,zttb_str);
            modelRes.pageHtml = LaomaPagerHelper.ShowPageNavigate(redirectParam, pageSize, pageIndex, totalCount);
            return Json(modelRes);
            #endregion

        }

        //退出
        public ActionResult ExitP()
        {
            Session.Clear();
            return RedirectToAction("login", "login");
        }

        //酒店报表填报界面
        public ActionResult JDBaoBiaoTianBao()
        {
            #region 有效性检查
            string bbztid_str = Request["bbztid"];
            int bbztid = -1;
            if (int.TryParse(bbztid_str, out bbztid) == false)
            {
                return Content("无效报表，请刷新后重试");
            }
            List<baobiao_zhuangtai> lisbbzt = bllm.Get_baobiao_zhuangtai(string.Format("id={0}", bbztid));
            if (lisbbzt == null || lisbbzt.Count <= 0)
            {
                return Content("无效报表，请刷新后重试");
            }
            //检查这个酒店是否属于这个用户的
            List<jiudian> lisjd = Self_GetMyJiuDian();
            if (lisjd.Exists(kkm => kkm.id == lisbbzt.First().jdid) == false)
            {
                return Content("抱歉，您无权查看");
            }
            //获取报表发布数据
            List<baobiao_fabu> lisbbfb = bllm.Get_baobiao_fabu(string.Format("id={0}", lisbbzt.First().bbid));
            #endregion

            #region 返回界面
            ViewBag.jd_name = lisjd.Find(kkm => kkm.id == lisbbzt.First().jdid).jd_name;
            ViewBag.bb_name = lisbbfb.First().bb_name;
            if (lisbbfb.First().leixing == 1)
            {
                #region 月报
                //判断是否存在，存在则展示数据，不存在则不展示
                List<yuebao> lisyb = bllm.Get_yuebao(string.Format("bbid={0} and jdid={1} order by tianbao_shijian desc limit 0,1",lisbbfb.First().id,lisbbzt.First().jdid));
                if (lisyb == null || lisyb.Count <= 0)
                {
                    #region 填写
                    DTO_yuebao modelyb = new DTO_yuebao();
                    modelyb.bbid = lisbbfb.First().id;
                    modelyb.jdid = lisbbzt.First().jdid;
                    modelyb.shenhe_zhuangtai = 0;
                    modelyb.yyjdid = -1;
                    modelyb.yywltid = -1;
                    modelyb.fabu_shijian_jiezhi = lisbbfb.First().tianbao_jieshu.ToString("yyyy-MM-dd");
                    modelyb.fabu_shijian_kaishi = lisbbfb.First().tianbao_kaishi.ToString("yyyy-MM-dd");
                    modelyb.baobiao_mingcheng = lisbbfb.First().bb_name;
                    modelyb.has_zanting = false;
                    modelyb.bb_riqi = lisbbfb.First().bb_riqi.ToString("yyyy-MM-dd");

                    ViewBag.modelYB = JsonConvert.SerializeObject(modelyb);
                    ViewBag.showData = 1;
                    #endregion
                }
                else
                {
                    #region 展示
                    ViewBag.modelYB = JsonConvert.SerializeObject(lisyb.First());
                    ViewBag.showData =0;
                    #endregion
                }
                return View("JDYueBaoTianBao");
                #endregion
            }
            else if (lisbbfb.First().leixing == 2)
            {
                #region 年报
                //判断是否存在，存在则展示数据，不存在则不展示
                List<nianbao> lisnb = bllm.Get_nianbao(string.Format("bbid={0} and jdid={1} order by tianbao_shijian desc limit 0,1", lisbbfb.First().id, lisbbzt.First().jdid));
                if (lisnb == null || lisnb.Count <= 0)
                {
                    #region 填写
                    DTO_nianbao modelnb = new DTO_nianbao();
                    modelnb.bbid = lisbbfb.First().id;
                    modelnb.jdid = lisbbzt.First().jdid;
                    modelnb.shenhe_zhuangtai = 0;
                    modelnb.yyjdid = -1;
                    modelnb.yywltid = -1;
                    modelnb.fabu_shijian_jiezhi = lisbbfb.First().tianbao_jieshu.ToString("yyyy-MM-dd");
                    modelnb.fabu_shijian_kaishi = lisbbfb.First().tianbao_kaishi.ToString("yyyy-MM-dd");
                    modelnb.baobiao_mingcheng = lisbbfb.First().bb_name;
                    modelnb.has_zanting = false;
                    modelnb.bb_riqi = lisbbfb.First().bb_riqi.ToString("yyyy-MM-dd");

                    ViewBag.modelNB = JsonConvert.SerializeObject(modelnb);
                    ViewBag.showData = 1;
                    #endregion
                }
                else
                {
                    #region 展示
                    ViewBag.modelNB = JsonConvert.SerializeObject(lisnb.First());
                    ViewBag.showData = 0;
                    #endregion
                }
                return View("JDNianBaoTianBao");
                #endregion
            }
            else
            {
                return Content("无此类报表");
            }
            #endregion
        }

        //酒店年报填报处理
        public ActionResult JDNianBaoTianBaoP()
        {
            #region 权限判断
            if (Self_CheckQX() == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "抱歉，您没有操作权限！", false, ""));
            }
            #endregion

            #region 存放方法变量
            string modelnb_str = Request["modelnb"];
            nianbao modelres = new nianbao();
            modelres = JsonConvert.DeserializeObject<nianbao>(modelnb_str);
            yonghu_jiudian modelyh = GetCurrentUser();
            DateTime dtNow = DateTime.Now;
            #endregion

            #region 有效性校验

            #endregion

            #region 报表重复性校验
            List<nianbao> lisbbnb = bllm.Get_nianbao(string.Format("bbid={0} and jdid={1} and shenhe_zhuangtai<=1", modelres.bbid, modelres.jdid));
            if (lisbbnb != null && lisbbnb.Count > 0)
            {
                return Json(CommonHelper.Self_GetRes(-1, "当前报表已填报", false, ""));
            }
            #endregion

            #region 数据补充
            modelres.yyjdid = modelyh.id;
            modelres.tianbiao_ren = modelyh.xingming;
            modelres.tianbiao_ren_dianhua = modelyh.zhanghao;
            modelres.tianbao_shijian = dtNow;

            #endregion

            #region 修改baobiao_zhuangtai表
            baobiao_zhuangtai modelbbzt = new baobiao_zhuangtai();
            modelbbzt.bbid = modelres.bbid;
            modelbbzt.dt_tianbao = modelres.tianbao_shijian;
            modelbbzt.jdid = modelres.jdid;
            modelbbzt.yyjdid = modelres.yyjdid;
            modelbbzt.zt_tianbao = 1;

            #endregion

            #region 保存到数据库
            int res = bllm.Add_nianbao(modelres, modelbbzt);
            if (res > 0)
            {
                return Json(CommonHelper.Self_GetRes(1, "填报成功", false, ""));
            }
            else
            {
                return Json(CommonHelper.Self_GetRes(-1, "填报失败，请重试", false, ""));
            }
            #endregion


        }

        //酒店月报填报处理
        public ActionResult JDYueBaoTianBaoP()
        {
            #region 权限判断
            if (Self_CheckQX() == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "抱歉，您没有操作权限！", false, ""));
            }
            #endregion

            #region 存放方法变量
            string modelyb_str = Request["modelyb"];
            yuebao modelres = new yuebao();
            modelres = JsonConvert.DeserializeObject<yuebao>(modelyb_str);
            yonghu_jiudian modelyh = GetCurrentUser();
            DateTime dtNow = DateTime.Now;
            #endregion

            #region 有效性校验

            #endregion

            #region 报表重复性校验
            List<yuebao> lisbbnb = bllm.Get_yuebao(string.Format("bbid={0} and jdid={1} and shenhe_zhuangtai<=1", modelres.bbid, modelres.jdid));
            if (lisbbnb != null && lisbbnb.Count > 0)
            {
                return Json(CommonHelper.Self_GetRes(-1, "当前报表已填报", false, ""));
            }
            #endregion

            #region 数据补充
            modelres.yyjdid = modelyh.id;
            modelres.tianbiao_ren = modelyh.xingming;
            modelres.tianbiao_ren_dianhua = modelyh.zhanghao;
            modelres.tianbao_shijian = dtNow;

            #endregion

            #region 修改baobiao_zhuangtai表
            baobiao_zhuangtai modelbbzt = new baobiao_zhuangtai();
            modelbbzt.bbid = modelres.bbid;
            modelbbzt.dt_tianbao = modelres.tianbao_shijian;
            modelbbzt.jdid = modelres.jdid;
            modelbbzt.yyjdid = modelres.yyjdid;
            modelbbzt.zt_tianbao = 1;

            #endregion

            #region 保存到数据库
            int res = bllm.Add_yuebao(modelres, modelbbzt);
            if (res > 0)
            {
                return Json(CommonHelper.Self_GetRes(1, "填报成功", false, ""));
            }
            else
            {
                return Json(CommonHelper.Self_GetRes(-1, "填报失败，请重试", false, ""));
            }
            #endregion


        }

        //获取酒店的用户信息
        public ActionResult JDGetUserP()
        {
            List<DTO_yonghu_jiudian> lisres = new List<DTO_yonghu_jiudian>();
            yonghu_jiudian modelyh_mother = new yonghu_jiudian();
            modelyh_mother = Self_GetMotherJDYH();
            if (modelyh_mother == null)
            {
                return Json(lisres);
            }
            List<jiudian> lisjd_mother = bllm.Get_jiudian(JsonConvert.DeserializeObject<List<int>>(modelyh_mother.jdid_json), false);

            List<yonghu_jiudian> lisyh = bllm.Get_yonghu_jiudian(string.Format("id={0} or mother_id={0} and zhuangtai<=2", modelyh_mother.id));

            lisres = ToDTO.Self_ToYHJDShow(lisjd_mother, lisyh);

            return Json(lisres);

        }

        //处理酒店用户的状态信息
        public ActionResult JDYHZhuangTaiP()
        {
            #region 权限判断
            if (Self_CheckQX() == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "抱歉，您没有操作权限！", false, ""));
            }
            #endregion

            #region 存放方法变量
            string yhjdid_str = Request["yhjdid"];
            string zt_str = Request["zt"];
            int yhjdid = -1;
            int zt = 1;
            #endregion

            #region 有效性判断
            if (int.TryParse(yhjdid_str, out yhjdid) == false)
            { 
                return Json(CommonHelper.Self_GetRes(-1,"无效用户",false,""));
            }
            if (int.TryParse(zt_str, out zt) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "无效操作", false, ""));
            }
            else if (zt >= 4 || zt <= 0)
            {
                return Json(CommonHelper.Self_GetRes(-1, "无效操作", false, ""));
            }

            yonghu_jiudian modelyh = GetCurrentUser();
            List<yonghu_jiudian> lisyhjd = bllm.Get_yonghu_jiudian(string.Format("id={0}", yhjdid));
            if (lisyhjd == null || lisyhjd.Count <= 0)
            {
                return Json(CommonHelper.Self_GetRes(-1, "无效用户", false, ""));
            }
            else if(lisyhjd.First().leixing==1)
            {
                return Json(CommonHelper.Self_GetRes(-1, "禁止操作主账户", false, ""));
            }

            #endregion

            #region 操作数据库
            int res = bllm.Modify_yonghu_jiudian_zt(yhjdid, zt);
            if (res > 0)
            {
                return Json(CommonHelper.Self_GetRes(1, "操作成功！", false, ""));
            }
            else
            {
                return Json(CommonHelper.Self_GetRes(-1, "操作失败，请刷新重试！", false, ""));
            }
            #endregion



        }

        //处理酒店用户的基本信息 添加子账户，修改账户
        public ActionResult JDYHShowP()
        {
            #region 权限判断
            if (Self_CheckQX() == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "抱歉，您没有操作权限！", false, ""));
            }
            #endregion

            #region 检查权限
            yonghu_jiudian modelcc = GetCurrentUser();
            
            #endregion

            #region 获取界面用户数据
            string modelyh_dto_str = Request["modelyh"];
            DTO_yonghu_jiudian modelyh_dto = new DTO_yonghu_jiudian();
            modelyh_dto = JsonConvert.DeserializeObject<DTO_yonghu_jiudian>(modelyh_dto_str); 
            #endregion

            #region 检查数据是否有效
            string dxyzm = Request["dxyzm"];
            if (string.IsNullOrEmpty(modelyh_dto.xingming) || string.IsNullOrWhiteSpace(modelyh_dto.xingming))
            {
                return Json(CommonHelper.Self_GetRes(-1, "请输入姓名！", false, ""));
            }
            if (string.IsNullOrEmpty(modelyh_dto.zhanghao))
            {
                return Json(CommonHelper.Self_GetRes(-1, "请输入电话号码！", false, ""));
            }
            else
            {
                if (Regex.IsMatch(modelyh_dto.zhanghao, @"^1[3456789]\d{9}$") == false)
                {
                    return Json(CommonHelper.Self_GetRes(-1, "无效电话号码！", false, ""));
                }
                if (Session["dxphone"] != null && string.IsNullOrEmpty(Session["dxphone"].ToString()) == false && Self_CheckImgYZM("dxphone", modelyh_dto.zhanghao) == false)
                {
                    //更换了电话号码，需要重新发送短信
                    Session["dxyzm"] = "";
                    return Json(CommonHelper.Self_GetRes(-1, "请重新发送短信验证码！", false, ""));
                }
            }

            if (string.IsNullOrEmpty(dxyzm))
            {
                return Json(CommonHelper.Self_GetRes(-1, "请输入短信验证码！", false, ""));
            }
            else
            {
                if (Self_CheckImgYZM("dxyzm", dxyzm) == false)
                {
                    return Json(CommonHelper.Self_GetRes(-1, "短信验证码错误！", false, ""));
                }
            }
            #endregion
            if (modelyh_dto.id <= 0)
            {
                #region 添加子账号
                //账号是否存在
                string whereStr = " zhanghao='" + modelyh_dto.zhanghao + "' AND zhuangtai<=2 ";
                List<yonghu_jiudian> lisyh_exist = bllm.Get_yonghu_jiudian(whereStr);
                if (lisyh_exist != null && lisyh_exist.Count > 0)
                {
                    return Json(CommonHelper.Self_GetRes(-1, "抱歉！当前账号已被占用，请更换账号后重试", false, ""));
                }

                //添加子账号数据
                yonghu_jiudian modeladd = new yonghu_jiudian();
                modeladd.guid_jdyh = "";
                modeladd.jdid_json = JsonConvert.SerializeObject(modelyh_dto.lisjdid);
                modeladd.leixing = 2;
                modeladd.mother_id = modelcc.id;
                modeladd.xingming = modelyh_dto.xingming;
                modeladd.zhanghao = modelyh_dto.zhanghao;
                modeladd.zhuangtai = 1;

                int res = bllm.Add_yonghu_jiudian(modeladd);
                if (res > 0)
                {
                    //清理短信计时
                    Session["dxtime"] = null;
                    return Json(CommonHelper.Self_GetRes(1, "操作成功", false, ""));
                }
                else
                {
                    return Json(CommonHelper.Self_GetRes(-1, "操作失败！请刷新后重试", false, ""));
                }
                #endregion
            }
            else
            {
                #region 修改用户
                //账号是否存在
                string whereStr = " zhanghao='" + modelyh_dto.zhanghao + "' AND zhuangtai<=2 AND id<>"+modelyh_dto.id;
                List<yonghu_jiudian> lisyh_exist = bllm.Get_yonghu_jiudian(whereStr);
                if (lisyh_exist != null && lisyh_exist.Count > 0)
                {
                    return Json(CommonHelper.Self_GetRes(-1, "抱歉！当前账号已被占用，请更换账号后重试", false, ""));
                }

                //修改账号数据
                yonghu_jiudian modelmodify = new yonghu_jiudian();
                modelmodify.id = modelyh_dto.id;
                if (modelcc.id == modelyh_dto.id)
                {
                    //主账户的酒店数据不允许修改
                    modelmodify.jdid_json = modelcc.jdid_json;
                }
                else
                {
                    modelmodify.jdid_json = JsonConvert.SerializeObject(modelyh_dto.lisjdid);
                }
                modelmodify.xingming = modelyh_dto.xingming;
                modelmodify.zhanghao = modelyh_dto.zhanghao;

                int res = bllm.Modify_yonghu_jiudian_info(modelmodify);
                if (res > 0)
                {
                    //清理短信计时
                    Session["dxtime"] = null;
                    return Json(CommonHelper.Self_GetRes(1, "操作成功", false, ""));
                }
                else
                {
                    return Json(CommonHelper.Self_GetRes(-1, "操作失败！请刷新后重试", false, ""));
                }

                #endregion
            }

        }

        //获取酒店统计数据
        public ActionResult GetJDTongJiP()
        {
            //获取当前账号的酒店数据
            yonghu_jiudian modelyhjd = GetCurrentUser();
            //modelyhjd = Self_GetMotherJDYH();
            List<int> lisjdid = JsonConvert.DeserializeObject<List<int>>(modelyhjd.jdid_json);
            List<jiudian> lisjd = bllm.Get_jiudian(lisjdid, false);

            //获取统计数据
            List<DTO_JDTongJi> listj = bllm.Get_DTO_JDTongJi(lisjdid);
            if (listj == null)
            {
                listj = new List<DTO_JDTongJi>();
            }
            jiudian modeljd_tmp = new jiudian();
            foreach (DTO_JDTongJi tj in listj)
            {
                modeljd_tmp = lisjd.Where(kkm => kkm.id == tj.jdid).FirstOrDefault();
                tj.jd_name = modeljd_tmp == null ? "" : modeljd_tmp.jd_name;
            }
            return Json(listj);
        }

        //保存注册页面数据
        public ActionResult ZhuCeP()
        {
            #region 权限判断
            if (Self_CheckQX() == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "抱歉，您没有操作权限！", false, ""));
            }
            #endregion

            string modeljdstr = Request["modeljd"];

            #region 存放方法变量
            
            DTO_jiudian modeljd_dto = new DTO_jiudian();
            modeljd_dto = JsonConvert.DeserializeObject<DTO_jiudian>(modeljdstr);
            //存放默认时间
            DateTime dt2000 = DateTime.Parse("2000-01-01");
            string dt2000str = "2000-01-01";
            DateTime dtnow = DateTime.Now;

            yonghu_jiudian modelyh_dto = GetCurrentUser();


            #endregion


            #region 酒店字段有效性判断
            string imgKeyWord = "pms-wenlvting";
            if (modeljd_dto.img_yingye_zhizhao.Contains(imgKeyWord) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "注册失败！请上传 营业执照", false, ""));
            }
            if (modeljd_dto.img_faren_shenfenzheng_fan.Contains(imgKeyWord) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "注册失败！请上传 法人身份证反面照", false, ""));
            }
            if (modeljd_dto.img_faren_shenfenzheng_zheng.Contains(imgKeyWord) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "注册失败！请上传 法人身份证正面照", false, ""));
            }
            if (modeljd_dto.img_shenqingren_sfz_fan.Contains(imgKeyWord) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "注册失败！请上传 申请人身份证反面照", false, ""));
            }
            if (modeljd_dto.img_shenqingren_sfz_zheng.Contains(imgKeyWord) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "注册失败！请上传 申请人身份证正面照", false, ""));
            }
            if (modeljd_dto.has_tehang_xukezheng && modeljd_dto.img_tezhong_hangye.Contains(imgKeyWord) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "注册失败！请上传 特种行业证件照", false, ""));
            }
            if (string.IsNullOrEmpty(modeljd_dto.jd_name))
            {
                return Json(CommonHelper.Self_GetRes(-1, "注册失败！请填写 旅馆、名宿名称", false, ""));
            }
            if (string.IsNullOrEmpty(modeljd_dto.tongyi_xinyong_daima))
            {
                return Json(CommonHelper.Self_GetRes(-1, "注册失败！请填写 统一社会信用代码", false, ""));
            }
            DateTime dt_kaiye_riqi = dt2000;
            if (DateTime.TryParse(modeljd_dto.kaiye_riqi, out dt_kaiye_riqi) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "注册失败！请填写 开业时间", false, ""));
            }
            DateTime dt_pingxing_riqi = dt2000;
            if (modeljd_dto.is_xingji && DateTime.TryParse(modeljd_dto.pingxing_riqi, out dt_pingxing_riqi) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "注册失败！请填写 评星时间", false, ""));
            }
            else
            {
                modeljd_dto.pingxing_riqi = dt2000str;
            }
            #endregion


            #region 酒店是否存在
            string whereStr = string.Format("jd_name='{0}' and shenhe_zhuangtai<=1", modeljd_dto.jd_name);
            List<jiudian> lisjiudian = bllm.Get_jiudian(whereStr);
            if (lisjiudian != null && lisjiudian.Count > 0)
            {
                DTO_Res msg = CommonHelper.Self_GetRes(-1, "酒店已存在，请直接登录！", false, "");
                return Json(msg);
            }
            #endregion

            #region 酒店其他默认字段
            modeljd_dto.shenhe_zhuangtai = 0;
            modeljd_dto.shenhe_shijian = dt2000str;
            modeljd_dto.yingye_zhuangtai = 0;
            modeljd_dto.yingye_zhuangtai_shijian = dt2000str;
            modeljd_dto.shenqing_shijian = dtnow.ToString("yyyy-MM-dd HH:mm:ss");
            modeljd_dto.suoshu_pinpai_guojia = modeljd_dto.suoshu_pinpai_guojia == null ? "" : modeljd_dto.suoshu_pinpai_guojia;
            modeljd_dto.chuanzhen = modeljd_dto.chuanzhen == null ? "" : modeljd_dto.chuanzhen;
            modeljd_dto.shenhe_jujue_shuoming = modeljd_dto.shenhe_jujue_shuoming == null ? "" : modeljd_dto.shenhe_jujue_shuoming;
            modeljd_dto.yingye_zhuangtai_shuoming = modeljd_dto.yingye_zhuangtai_shuoming == null ? "" : modeljd_dto.yingye_zhuangtai_shuoming;
            
            #endregion

            #region 转换数据
            jiudian modeljd = new jiudian();
            modeljd = JsonConvert.DeserializeObject<jiudian>(JsonConvert.SerializeObject(modeljd_dto));
            
            #endregion

            #region 用户和酒店的关联标志
            modeljd.guid_jdyh = modelyh_dto.guid_jdyh;

            #endregion

            #region 将数据写入数据库中
            int res = bllm.Add_ZhuCeSub(modeljd);
            if (res > 0)
            {
                return Json(CommonHelper.Self_GetRes(1, "注册成功！", false, "/JiuDian/JDInfo"));
            }
            else
            {
                return Json(CommonHelper.Self_GetRes(1, "注册失败！", false, ""));
            }
            #endregion


        }

        //获取待填报消息数量
        public ActionResult GetMsgInfoCount()
        {
            yonghu_jiudian modelyhjd = Self_GetMotherJDYH();
            List<int> lisjdid = JsonConvert.DeserializeObject<List<int>>(modelyhjd.jdid_json);
            if (lisjdid.Count <= 0)
            {
                return Content("0");
            }
            int totalCount = bllm.Get_BBZhuangTaiCount(string.Format("jdid IN ({0}) AND (zt_tianbao=0 OR zt_shenhe=2) AND isdel=FALSE",string.Join(",",lisjdid)));
            return Content(totalCount.ToString());
        }

        //获取待填报消息明细，前50条消息
        public ActionResult GetMsgInfoDetail()
        {
            List<DTO_JDMsg> lismsg = new List<DTO_JDMsg>();
            yonghu_jiudian modelyhjd = Self_GetMotherJDYH();
            List<int> lisjdid = JsonConvert.DeserializeObject<List<int>>(modelyhjd.jdid_json);
            if (lisjdid.Count <= 0)
            {
                return Json(lismsg);
            }
            //获取报表状态数据
            List<baobiao_zhuangtai> liszt = new List<baobiao_zhuangtai>();
            liszt = bllm.Get_baobiao_zhuangtai(string.Format("jdid IN ({0}) AND (zt_tianbao=0 OR zt_shenhe=2) AND isdel=FALSE", string.Join(",", lisjdid)));
            if (liszt == null || liszt.Count <= 0)
            {
                return Json(lismsg);
            }
            List<jiudian> lisjd = Self_GetMyJiuDian();
            if (lisjd == null)
            {
                lisjd = new List<jiudian>();
            }
            List<baobiao_fabu> lisfb = bllm.Get_baobiao_fabu(string.Format("id in ({0})", string.Join(",", liszt.Select(kkm => kkm.bbid))));
            if (lisfb == null)
            {
                lisfb = new List<baobiao_fabu>();
            }
            //转换数据
            lismsg = ToDTO.Self_ToJDMsg(lisjd, lisfb, liszt);
            return Json(lismsg);
        }



        #region 自定义方法区

        /// <summary>
        /// 获取当前用户的酒店信息
        /// </summary>
        /// <returns></returns>
        private List<jiudian> Self_GetMyJiuDian()
        {
            List<int> lisjdid = new List<int>();
            lisjdid = JsonConvert.DeserializeObject<List<int>>(GetCurrentUser().jdid_json);
            List<jiudian> lisjd = bllm.Get_jiudian(lisjdid, false);

            return lisjd;
        }

        /// <summary>
        /// 获取当前用户的酒店数据
        /// </summary>
        /// <returns></returns>
        private List<DTO_JDShort> Self_GetMyJDShort()
        {
            return ToDTO.Self_JDToJDShort(Self_GetMyJiuDian());
        }

        /// <summary>
        /// 获取主账号酒店用户数据
        /// </summary>
        /// <returns></returns>
        private yonghu_jiudian Self_GetMotherJDYH()
        {
            yonghu_jiudian modelyh = GetCurrentUser();
            yonghu_jiudian modelyh_mother = new yonghu_jiudian();
            if (modelyh.leixing == 1)
            {
                modelyh_mother = modelyh;
            }
            else
            {
                List<yonghu_jiudian> lisyhjd_main = new List<yonghu_jiudian>();
                lisyhjd_main = bllm.Get_yonghu_jiudian(string.Format("id={0}", modelyh.mother_id));
                if (lisyhjd_main == null || lisyhjd_main.Count <= 0)
                {
                    return null;
                }
                else
                {
                    modelyh_mother = lisyhjd_main.First();
                }
            }
            return modelyh_mother;
        }

        /// <summary>
        /// 检查验证码是否一致
        /// </summary>
        /// <param name="sessionName">session中存放的名字</param>
        /// <param name="yzm"></param>
        /// <returns></returns>
        public bool Self_CheckImgYZM(string sessionName, string yzm)
        {
            string sess_yzm = Session[sessionName] as string;
            if (string.IsNullOrEmpty(sess_yzm) || string.IsNullOrEmpty(yzm))
            {
                return false;
            }
            if (sess_yzm.ToLower() != yzm.ToLower())
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 判断当前用户是否有操作权限
        /// </summary>
        /// <returns></returns>
        private bool Self_CheckQX()
        {
            if (GetCurrentUser().leixing == 2)
            {
                return false;
            }
            return true;
        }

        #endregion



    }
}
